package cn.iocoder.yudao.module.promotion.fod.api;

import cn.iocoder.yudao.module.api.promotion.fod.FirstOrderDiscountApi;
import cn.iocoder.yudao.module.api.promotion.fod.bo.FirstOrderDiscountBO;
import cn.iocoder.yudao.module.api.promotion.fod.enums.FirstOrderDiscountTypeEnum;
import cn.iocoder.yudao.module.promotion.fod.service.FirstOrderDiscountConfigService;
import cn.iocoder.yudao.module.promotion.fod.service.FirstOrderDiscountNewUserService;
import cn.iocoder.yudao.module.promotion.fod.service.FirstOrderDiscountVipRechargeService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

/**
 * 平台首单优惠活动
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/03/25
 */
@Service
@Validated
public class FirstOrderDiscountApiImpl implements FirstOrderDiscountApi {
    @Resource
    private FirstOrderDiscountNewUserService firstOrderDiscountNewUserService;
    @Resource
    private FirstOrderDiscountVipRechargeService firstOrderDiscountVipRechargeService;
    @Resource
    private FirstOrderDiscountConfigService firstOrderDiscountConfigService;

    @Override
    public boolean hasEligibility(Long userId, FirstOrderDiscountTypeEnum type) {
        if (FirstOrderDiscountTypeEnum.NEW_VIP.equals(type)) {
            return firstOrderDiscountVipRechargeService.hasEligibility(userId);
        }
        return firstOrderDiscountNewUserService.hasEligibility(userId);
    }

    @Override
    public void applyPlatformDiscount(Long userId, String tradeNo, Integer originalPrice, Integer discountPrice) {
        if (hasEligibility(userId, FirstOrderDiscountTypeEnum.NEW_VIP)) {
            firstOrderDiscountVipRechargeService.applyDiscount(FirstOrderDiscountTypeEnum.NEW_VIP, userId, tradeNo, originalPrice, discountPrice);
            return;
        }
        if (hasEligibility(userId, FirstOrderDiscountTypeEnum.NEW_USER)) {
            firstOrderDiscountNewUserService.applyDiscount(FirstOrderDiscountTypeEnum.NEW_USER, userId, tradeNo, originalPrice, discountPrice);
        }
    }

    @Override
    public List<FirstOrderDiscountBO> getDiscountConfig(Long shopId) {
        return firstOrderDiscountConfigService.getList(shopId);
    }

}
